package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.*;
import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpLogMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.service.EmpLogService;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private EmpExprMapper empExprMapper;
    @Autowired
    private EmpLogService empLogService;
//    @Override
//    public PageBean page(Integer page, Integer pageSize) {
////        Long total = empMapper.count();
////        Integer start = (page - 1) * pageSize;
////        List<Emp> rows = empMapper.page(start, pageSize);
////        return new PageBean(total, rows);
//
//        //1. 设置分页参数
//        PageHelper.startPage(page, pageSize);
//
//        List<Emp> empList = empMapper.list();
//        Page<Emp> empPage = (Page<Emp>) empList;
//        return new PageBean(empPage.getTotal(), empPage.getResult());
//    }

    @Override
    public PageBean page(EmpQueryParam empQueryParam) {
        PageHelper.startPage(empQueryParam.getPage(), empQueryParam.getPageSize());
        List<Emp> empList = empMapper.list(empQueryParam);
        Page<Emp> empPage = (Page<Emp>) empList;
        return new PageBean(empPage.getTotal(), empPage.getResult());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(Emp emp) {
        try {
            emp.setCreateTime(LocalDateTime.now());
            emp.setUpdateTime(LocalDateTime.now());

            empMapper.save(emp);

            int i = 1 / 0;

            Integer empId = emp.getId();
            List<EmpExpr> exprList = emp.getExprList();
            if(!CollectionUtils.isEmpty(exprList)){
                exprList.forEach(expr -> {
                    expr.setEmpId(empId);
                    empExprMapper.insertBatch(exprList);
                });
            }
        } finally {
            //记录操作日志
            EmpLog empLog = new EmpLog(null, LocalDateTime.now(), emp.toString());
            empLogService.insertLog(empLog);
        }
    }

    @Override
    public void deleteByIds(List<Integer> ids) {
        empMapper.deleteByIds(ids);

        empExprMapper.deleteByIds(ids);
    }

    @Override
    public Emp getById(Integer id) {
        return empMapper.getById(id);
    }

    @Transactional
    public void update(Emp emp) {
        //1.根据id更新员工基本信息
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateById(emp);
        //2.根据员工id删除员工工作经历信息（老的）
        empExprMapper.deleteByEmpId(emp.getId());
        //3.根据员工id插入员工新的工作经历信息（新的）
        Integer empId = emp.getId();
        List<EmpExpr> exprList = emp.getExprList();
        if(!CollectionUtils.isEmpty(exprList)){
            exprList.forEach(expr -> {
                expr.setEmpId(empId);
                empExprMapper.insertBatch(exprList);
            });
        }
    }
}
